// JavaScript Document
$(document).ready(function() { 
	$('.session').load("/interface/modules/login/index.php");
	
	$('#menu ol li').live('click',function(){
		var page = $(this).attr('class');
		if(page != "" && page != 'undefided' && page != 'home'){
			$.post('/interface/modules/' + page + '/index.php',{},function(data){
				$('#left').html(data);
			});
		}
	
	});
	
	
	
	/****************
	*	SENDFORM	*
	****************/		
		
	
	$('.sendForm .list table tr').live('click',function(){
		var id = $(this).attr('id');
		var firstElement = $('#' + id + " td:nth-child(1)").text();
		var secondElement = $('#' + id + " td:nth-child(2) input").attr('value');
		
		$('.sendForm .list table .selected').removeClass('selected');
		$(this).addClass('selected');
		
		$('input[name=description]').attr('value',firstElement);
		$('.subIndex').attr('value',secondElement);
		$('input[name=id]').attr('value',id);
		$('input[class=new]').hide();
		$('.sendForm .actionButtons p').show();
		return false;
	})
	
	$('.sendForm .actionButtons span').live('click',function(){		
		$('.sendForm .actionButtons p').hide();
		$('input[class=new]').show();
		$('.subIndex').attr('value','null');
		$('input[name=description]').attr('value','');
		$('input[name=id]').attr('value','');
		$('.sendForm .list table .selected').removeClass('selected');
		
		return false;
	})
	
	$('.new').live('click',		function(){	sendForm('insert');	return false;	})
	$('.modify').live('click',	function(){	sendForm('update');	return false;	})
	$('.delete').live('click',	function(){	sendForm('delete');	return false;	})
	
	
	function sendForm(method){
		name = $('input[name=description]').attr('value');
			formString = $('.sendForm form').serialize();
			page = $('.sendForm form').attr('name');
			$.ajax({
				type:	"POST",
				url: 	"/interface/modules/"+page+"/index.php",
				data: 	"method=" + method  + "&" + formString,
				success: function(data){
					if(data == 1) {reloadPage(page);}
					else{ alert("Complete todos los campos");}
				}
			});		
		return false;
	}
	
	
	function reloadPage(page){
		$.post('/interface/modules/'+page+'/index.php',{},function(data){
			$('#left').html(data)
			return false;
		});
	}
	
	/* Registry */
	$('.listForm input[name=sendForm]').live('click',function(){
		formString = $('form[name=listForm]').serialize();
		$.ajax({
				type:	"POST",
				url: 	"/interface/modules/registry/index.php",
				data: 	"method=" + "insert"  + "&" + formString,
				success: function(data){
					if(data.substr(0,1) == 1) { alert("Se ha registrado correctamente"); document.location='/';}
					else{ alert("Complete todos los campos");}
				}
			});		
	})
	

})